<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>领取优惠券</title>
</head>
<body>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    function getParam(url, name) {
        try {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            let r = url.split('?')[1].match(reg);
            if (r != null) {
                return r[2];
            }
            return "";
        } catch (e) {
            return "";
        }
    }

    let url = window.location.href;
    let storeId = getParam(url, 'storeId');
    let couponId = getParam(url, 'couponId');
    $.ajax({
        url: "http://localhost:8080/coupon.do",
        type: "post",
        data: {
            couponId: couponId,
            storeId: storeId,
            method: 'receiveCoupon'
        },
        dataType: "text",
        success: function (data) {
            if (data === 'ok') {
                alert('领取成功');
                window.location.href = '/User/UserDishPage.jsp?storeId=' + storeId;
            }
            if (data === 'login') {
                alert('请先登录！');
                window.location.href = 'DishPage.jsp?storeId=' + storeId;
            }
            if (data === 'error') {
                alert('领取失败');
                window.location.href = '/User/UserDishPage.jsp?storeId=' + storeId;
            }
            if (data === 'exist') {
                alert('您已领取该优惠券，请先使用！');
                window.location.href = '/User/UserDishPage.jsp?storeId=' + storeId;
            }
        }
    })
</script>
</body>
</html>
